﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class ThemNhanVien : Form
    {
        string filename;
        string NgaySinh;
        string tenanh;

        public ThemNhanVien()
        {
            InitializeComponent();
        }

        private void xoaThongTin()
        {
            HoTen_NV.Text = "";
            NgayComboBox.Text = "";
            ThangCombobox.Text = "";
            NamCombobox.Text = "";
            radioButton1.Checked = false;
            radioButton2.Checked = false;
            CMND_NV.Text = "";
            SDT_NV.Text = "";
            Email_NV.Text = "";
            DiaChi_NV.Text = "";
            ChucVuCombobox.Text = ""; 
            TenDangNhap_NV.Text = "";
            MatKhau_NV.Text = "";
            AnhNhanVien.Image = null;
        }

        private void quayLaiButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void loadcomboboxChucVu(string MSPB)
        {
            MSPB = PhongBanCombobox.SelectedValue.ToString();
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from ChucVu where MaSo_PB = @MaSo_PB";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@MaSo_PB", MSPB);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "ChucVu");
            ChucVuCombobox.DisplayMember = "Ten_CV";
            ChucVuCombobox.ValueMember = "MaSo_CV";
            ChucVuCombobox.DataSource = ds.Tables["ChucVu"].DefaultView;
        }

        private void themNhanVien(string HoTenNV, string NgaySinhNV, bool gioiTinhNV, string CMND, string SDT, string Email, string DiaChiNV, string HinhAnhNV, string tenDangNhap, string matKhau, string chucVuNV)
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "insert into NhanVien(MaSo_NV,HoTen_NV,NgaySinh_NV,GioiTinh_NV,CMND_NV,SDT_NV,Email_NV,DiaChi_NV,HinhAnh_NV,TenDangNhap_NV,MatKhau_NV,MaSo_CV) values('',@HoTen_NV,@NgaySinh_NV,@GioiTinh_NV,@CMND_NV,@SDT_NV,@Email_NV,@DiaChi_NV,@HinhAnh_NV,@TenDangNhap_NV,@MatKhau_NV,@MaSo_CV)";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@HoTen_NV", HoTenNV);
            cmd.Parameters.AddWithValue("@NgaySinh_NV", NgaySinh);
            cmd.Parameters.AddWithValue("@GioiTinh_NV", gioiTinhNV);
            cmd.Parameters.AddWithValue("@CMND_NV", CMND);
            cmd.Parameters.AddWithValue("@SDT_NV", SDT);
            cmd.Parameters.AddWithValue("@Email_NV", Email);
            cmd.Parameters.AddWithValue("@DiaChi_NV", DiaChiNV);
            cmd.Parameters.AddWithValue("@HinhAnh_NV", HinhAnhNV);
            cmd.Parameters.AddWithValue("@TenDangNhap_NV", tenDangNhap);
            cmd.Parameters.AddWithValue("@MatKhau_NV", matKhau);
            cmd.Parameters.AddWithValue("@MaSo_CV", chucVuNV);
            cmd.ExecuteNonQuery();
            thongBaoLabel.Text = "Thêm thành công !";
            conn.Close();
        }

        private void ThemButton_Click(object sender, EventArgs e)
        {
            NgaySinh = ThangCombobox.Text + "/" + NgayComboBox.Text + "/" + NamCombobox.Text;
            bool gioitinh;
            if(radioButton1.Checked)
                gioitinh=true;
            else
                gioitinh=false;
            themNhanVien(HoTen_NV.Text, NgaySinh,gioitinh, CMND_NV.Text, SDT_NV.Text, Email_NV.Text, DiaChi_NV.Text, tenanh, TenDangNhap_NV.Text, MatKhau_NV.Text, ChucVuCombobox.SelectedValue.ToString());
            xoaThongTin();
            loadcomboboxChucVu(PhongBanCombobox.SelectedValue.ToString());
        }

        private void chonAnhButton_Click(object sender, EventArgs e)
        {
                OpenFileDialog f = new OpenFileDialog();
                f.InitialDirectory = @"E:\Bai hoc\Lap trinh web 1\quan-ly-trang-suc-m3t\source\Quan ly thong tin he thong\Quan ly thong tin he thong\Anh nhan vien";
                f.Filter = "Images Files (*.bmp, *jpg, *.png)|*.bmp; *.jpg; *.png|All Files (*.*)|*.*";
                if (f.ShowDialog() == DialogResult.OK)
                {
                    filename = f.FileName;
                    tenanh = System.IO.Path.GetFileName(f.FileName);
                    AnhNhanVien.Image = Image.FromFile(filename);
                }
        }

        private void xoaAnhButton_Click(object sender, EventArgs e)
        {
            AnhNhanVien.Image = null;
        }

        private void ThemNhanVien_Load(object sender, EventArgs e)
        {
            loadComboboxPhongBan();
        }

        private void loadComboboxPhongBan()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from PhongBan";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "PhongBan");
            PhongBanCombobox.DisplayMember = "Ten_PB";
            PhongBanCombobox.ValueMember = "MaSo_PB";
            PhongBanCombobox.DataSource = ds.Tables["PhongBan"].DefaultView;
        }

        private void PhongBanCombobox_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadcomboboxChucVu(PhongBanCombobox.SelectedValue.ToString());
        }
    }
}
